package fem2;

import math2.MathUtilities;

import org.junit.Assert;
import org.junit.Test;

public class MeshPartTest {

	@Test
	public void testComputeArea1() {
		Node n1 = new Node(0, 0);
		Node n2 = new Node(1, 0.5);
		Node n3 = new Node(0.5, 1);

		Face f = new Face(n1, n2, n3);
		double a = f.computeVolume();

		Assert.assertEquals(0.375, a, MathUtilities.tol14);
	}

	@Test
	public void testComputeArea2() {
		Node n1 = new Node(0, 0);
		Node n2 = new Node(1, 0.5);
		Node n3 = new Node(1, 1);
		Node n4 = new Node(0.5, 1);

		Face f = new Face(n1, n2, n3, n4);
		double a = f.computeVolume();

		Assert.assertEquals(0.5, a, MathUtilities.tol14);
	}

	@Test
	public void testFace() {
		Node n1 = new Node(0, 0);
		Node n2 = new Node(1, 0);
		Node n3 = new Node(1, 1);
		Node n4 = new Node(0, 1);
		Face f = new Face(n1, n2, n3, n4);
		Node[] nodes = f.getNodes();
		for (int i = 0; i < nodes.length; i++) {
			Debugger.watch("n" + i, nodes[i].getPosition());
		}
		nodes[0] = new Node(-1, 0);
		nodes = f.getNodes();
		for (int i = 0; i < nodes.length; i++) {
			Debugger.watch("n" + i, nodes[i].getPosition());
		}
	}

	@Test
	public void testXiAt1() {
		Node n1 = new Node(0, 0);
		Node n2 = new Node(1, 0);
		Node n3 = new Node(1, 1);
		Node n4 = new Node(0, 1);
		Face f = new Face(n1, n2, n3, n4);

		double[] x = new double[] { 0.5, 0.5 };
		double[] xi1 = f.XiAt(x);
		Debugger.watch(xi1);
		Debugger.watch(f.XAt(xi1));
		Assert.assertArrayEquals(x, f.XAt(xi1), MathUtilities.tol6);

		x = new double[] { 0.6, 0.5 };
		xi1 = f.XiAt(x);
		Debugger.watch(xi1);
		Debugger.watch(f.XAt(xi1));
		Assert.assertArrayEquals(x, f.XAt(xi1), MathUtilities.tol6);
	}

	@Test
	public void testXiAt2() {
		Node n1 = new Node(0, 0);
		Node n2 = new Node(1, 0);
		Node n3 = new Node(1, 1);
		Face f = new Face(n1, n2, n3);

		double[] x = new double[] { 0.5, 0.5 };
		double[] xi1 = f.XiAt(x);
		Debugger.watch(xi1);
		Debugger.watch(f.XAt(xi1));
		Assert.assertArrayEquals(x, f.XAt(xi1), MathUtilities.tol6);

		x = new double[] { 0.6, 0.5 };
		xi1 = f.XiAt(x);
		Debugger.watch(xi1);
		Debugger.watch(f.XAt(xi1));
		Assert.assertArrayEquals(x, f.XAt(xi1), MathUtilities.tol6);

	}
}
